package cn.edu.lcu.cs.ssm.mapper;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.time.LocalDate;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.lcu.cs.ssm.entity.User;
import cn.edu.lcu.cs.ssm.test.SpringTest;

/**
 * MyBatis与Spring集成测试
 * 
 * @author Ling
 *
 */
class UserMapperTestWithSpring extends SpringTest {
	@Autowired
	private UserMapper userMapper;

	@Test
	void testFindAll() {
		assertNotNull(userMapper);
		List<User> users = userMapper.findAll();
		assertNotNull(users);
		System.out.println(users);
	}

	@Test
	void testFindById() {
		long id = 1L;
		User user = userMapper.findById(id);
		System.out.println(user);
	}

	@Test
	// 将标注的类或者方法置于事务环境中
	// 在测试框架中，本注解默认让事务回滚，符合测试的原则。测试不能实质破坏数据库。
	@Transactional 
//	@Commit // 强制提交事务
//	@Rollback // 强制事务回滚
	void testSave() {
		User user = new User();
		user.setUsername("temeipu");
		user.setPassword("123");
		user.setRealName("特没谱");
		user.setBirthdate(LocalDate.now());
		int count = userMapper.save(user);
		assertTrue(count == 1);
		System.out.println(user);
	}

}
